package com.zhuojy.equity.mall.admin.web.controller.car;

import com.zhuojy.equity.mall.admin.common.entity.TCar;
import com.zhuojy.equity.mall.admin.common.entity.vo.salary.TUserArchivesVO;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.utils.ExcelUtils;
import com.zhuojy.equity.mall.admin.service.car.TCarService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xrz
 * @version 1.0
 * @description:TODO
 * @date 2024/3/27 17:13
 */
@RestController
@RequiredArgsConstructor
@CrossOrigin
@RequestMapping("car")
public class TCarController {
    @Autowired
    private TCarService tCarService;

    /**
     * 查询全部车辆信息
     * @param tCar
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping("getCar")
    private ResultEntity getCarList(@RequestBody TCar tCar, @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum")Integer pageNum){
        return tCarService.getCarList(tCar,pageSize,pageNum);
    }

    /**
     * 查询所有车牌号
     * @return
     */
    @RequestMapping("getCarNumber")
    private ResultEntity getCarNumber(){
        return tCarService.getCarNumber();
    }

    /**
     * 添加车辆信息
     * @param tCar
     * @return
     */
    @RequestMapping("addCar")
    private ResultEntity addCar(@RequestBody TCar tCar){
        return tCarService.addCar(tCar);
    }

    /**
     * 通过id查询车辆信息
     * @param id
     * @return
     */
    @RequestMapping("getCarById")
    private ResultEntity getCarById(@RequestParam("id") Integer id){
        return tCarService.getCarById(id);
    }

    /**
     * 修改车辆信息
     * @param tCar
     * @return
     */
    @RequestMapping("updateCar")
    private ResultEntity updateCar(@RequestBody TCar tCar){
        return tCarService.updateCar(tCar);
    }

    /**
     * 通过id 删除车辆信息
     * @param id
     * @return
     */
    @RequestMapping("deleteCarById")
    private ResultEntity deleteCarById(@RequestParam("id") Integer id){
        return tCarService.deleteCarById(id);
    }

    /**
     * 导出excel表格
     * @param response
     */
    @RequestMapping("excelexport")
    private void excelexport(HttpServletResponse response){
        List<TCar> list = tCarService.list();
        ExcelUtils.export(response,"车辆信息表格.xlsx",list, TUserArchivesVO.class);
    }
    @RequestMapping("excelpimport")
    private ResultEntity excelimport(MultipartFile file) throws Exception {
        try {
            List<TCar> tCars = ExcelUtils.readMultipartFile(file, TCar.class);
            for (TCar tCar:tCars){
                if (("空闲").equals(tCar.getCarState())){
                    tCar.setCarState("0");
                }else if (("使用中").equals(tCar.getCarState())){
                    tCar.setCarState("1");
                }else if (("审批拒绝").equals(tCar.getCarState())){
                    tCar.setCarState("2");
                }else if (("审批撤销").equals(tCar.getCarState())){
                    tCar.setCarState("3");
                }
                if (("燃油").equals(tCar.getFuelOiltype())){
                    tCar.setFuelOiltype("0");
                }else if (("电动").equals(tCar.getFuelOiltype())){
                    tCar.setFuelOiltype("1");
                } else if (("油电混合").equals(tCar.getFuelOiltype())){
                    tCar.setFuelOiltype("2");
                }
                tCarService.save(tCar);
            }
            return ResultEntity.OK("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.OK("导入失败");
        }
    }

}
